package com.gc.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gc.business.domain.*;
import com.gc.business.service.*;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.utils.DateUtils;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gc.business.mapper.UpkeepInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯保养 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-18
 */
@Slf4j
@Service
public class UpkeepInfoServiceImpl extends ServiceImpl<UpkeepInfoMapper, UpkeepInfo> implements IUpkeepInfoService {

    @Autowired
    private IElevatorInfoService elevatorInfoService;

    @Autowired
    private IUpkeepPlanService upkeepPlanService;

    @Autowired
    private IUpkeepInfoService upkeepInfoService;

    @Autowired
    private ICommunityManageService communityManageService;

    @Autowired
    private ICompanyManageService companyManageService;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<UpkeepInfo> list(UpkeepInfo upkeepInfo){
        LambdaQueryWrapper<UpkeepInfo> wrapper = new LambdaQueryWrapper<>();
        if(upkeepInfo != null){
            if(StringUtils.isNotEmpty(upkeepInfo.getUpkeepId())){
                wrapper.eq(UpkeepInfo::getUpkeepId, upkeepInfo.getUpkeepId());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getElevatorId())){
                wrapper.eq(UpkeepInfo::getElevatorId, upkeepInfo.getElevatorId());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getTemplateId())){
                wrapper.eq(UpkeepInfo::getTemplateId, upkeepInfo.getTemplateId());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUpkeepCode())){
                wrapper.eq(UpkeepInfo::getUpkeepCode, upkeepInfo.getUpkeepCode());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getElevatorName())){
                wrapper.like(UpkeepInfo::getElevatorName, upkeepInfo.getElevatorName());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getIdentifyCode())){
                wrapper.eq(UpkeepInfo::getIdentifyCode, upkeepInfo.getIdentifyCode());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUseUnit())){
                wrapper.like(UpkeepInfo::getUseUnit, upkeepInfo.getUseUnit());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getMaintenanceUnit())){
                wrapper.like(UpkeepInfo::getMaintenanceUnit, upkeepInfo.getMaintenanceUnit());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUpkeepType())){
                wrapper.eq(UpkeepInfo::getUpkeepType, upkeepInfo.getUpkeepType());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUpkeepState())){
                wrapper.eq(UpkeepInfo::getUpkeepState, upkeepInfo.getUpkeepState());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getApplicant())){
                wrapper.like(UpkeepInfo::getApplicant, upkeepInfo.getApplicant());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getOverTime())){
                wrapper.eq(UpkeepInfo::getOverTime, upkeepInfo.getOverTime());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getStartTime())){
                wrapper.eq(UpkeepInfo::getStartTime, upkeepInfo.getStartTime());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUpkeepDate())){
                wrapper.eq(UpkeepInfo::getUpkeepDate, upkeepInfo.getUpkeepDate());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getLongitude())){
                wrapper.eq(UpkeepInfo::getLongitude, upkeepInfo.getLongitude());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getLatitude())){
                wrapper.eq(UpkeepInfo::getLatitude, upkeepInfo.getLatitude());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getRoomImg())){
                wrapper.eq(UpkeepInfo::getRoomImg, upkeepInfo.getRoomImg());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getRoomRemark())){
                wrapper.like(UpkeepInfo::getRoomRemark, upkeepInfo.getRoomRemark());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getFloorImg())){
                wrapper.eq(UpkeepInfo::getFloorImg, upkeepInfo.getFloorImg());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getFloorRemark())){
                wrapper.eq(UpkeepInfo::getFloorRemark, upkeepInfo.getFloorRemark());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getCapsulesImg())){
                wrapper.eq(UpkeepInfo::getCapsulesImg, upkeepInfo.getCapsulesImg());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getCapsulesRemark())){
                wrapper.eq(UpkeepInfo::getCapsulesRemark, upkeepInfo.getCapsulesRemark());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getShaftImg())){
                wrapper.eq(UpkeepInfo::getShaftImg, upkeepInfo.getShaftImg());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getShaftRemark())){
                wrapper.eq(UpkeepInfo::getShaftRemark, upkeepInfo.getShaftRemark());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getCreateBy())){
                wrapper.eq(UpkeepInfo::getCreateBy, upkeepInfo.getCreateBy());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getCreateTime())){
                wrapper.eq(UpkeepInfo::getCreateTime, upkeepInfo.getCreateTime());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUpdateBy())){
                wrapper.eq(UpkeepInfo::getUpdateBy, upkeepInfo.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getUpdateTime())){
                wrapper.eq(UpkeepInfo::getUpdateTime, upkeepInfo.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(upkeepInfo.getRemark())){
                wrapper.eq(UpkeepInfo::getRemark, upkeepInfo.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }


    /**
     * 通过审核状态查询记录列表
     * @param reviewType
     * @param companyId 公司ID
     * @return
     */
    @Override
    public List<UpkeepInfo> selectListByReviewType(UpkeepInfo upkeepInfo,Integer reviewType,Long companyId){
        return baseMapper.selectListByReviewType(upkeepInfo,reviewType,companyId);
    }

    /**
     * 通过提交人/审核人查询自己相关的记录列表
     * @param referUserId
     * @param auditUserId
     * @return
     */
    @Override
    public List<UpkeepInfo> selectListByReviewUserId(Long referUserId,Long auditUserId){
        return baseMapper.selectListByReviewUserId(referUserId,auditUserId);
    }


    /**
     * 联查根据ID详情需要的信息
     * @param upkeepId
     * @return
     */
    @Override
    public UpkeepInfo selectAllInfoById(Long upkeepId){
        return baseMapper.selectAllInfoById(upkeepId);
    }

    /**
     * 查询如果需要保养的电梯进行保养(定时器每天凌晨1点执行一次)
     * @return
     */
    @Override
    public int saveUpkeepBusinessInfo(){

        //查询系统全部电梯
        List<ElevatorInfo> elevatorList = elevatorInfoService.list();

        for (ElevatorInfo elevatorInfo : elevatorList) {
            //查询对应的电梯保养计划
            UpkeepPlan elevatorPlan = upkeepPlanService.getByElevatorId(elevatorInfo.getElevatorId());

            //最近一次保养时间
            String recentlyTime = elevatorPlan.getRecentlyTime();
            //首次保养时间
            String firstUpkeepTime = elevatorPlan.getFirstUpkeepTime();
            //保养间隔时间
            Integer upkeepInterval = elevatorPlan.getUpkeepInterval();
            //提醒天数
            Integer remindDay = elevatorPlan.getRemindDay();

            if(StringUtils.isEmpty(recentlyTime)){
                recentlyTime = firstUpkeepTime;
            }
            if(upkeepInterval > remindDay){
                upkeepInterval = upkeepInterval - remindDay;
            }else{
                upkeepInterval = remindDay;
            }

            try {
                //获取保养时间
                Date upkeepDate = DateUtils.getDate(DateUtil.parseDate(recentlyTime), upkeepInterval);
                if(DateUtils.isToday(upkeepDate.getTime())){
                    //派发保养信息
                    UpkeepInfo upkeepInfo = new UpkeepInfo();
                    upkeepInfo.setElevatorId(elevatorInfo.getElevatorId());
                    upkeepInfo.setTemplateId(elevatorPlan.getTemplateId() == null ? 30L : elevatorPlan.getTemplateId());
                    upkeepInfo.setUpkeepCode("BY"+ IdUtils.getPrimaryKey());
                    upkeepInfo.setElevatorName(elevatorInfo.getElevatorName());
                    upkeepInfo.setIdentifyCode(elevatorInfo.getIdentifyCode());
                    //使用单位
                    List<CommunityManage> communityById = communityManageService.list(new CommunityManage(){{setCommunityId(Long.valueOf(elevatorInfo.getBelongCommunity()));}});
                    upkeepInfo.setUseUnit(communityById.get(0).getCommunityName());
                    //维保单位
                    List<CompanyManage> companyById = companyManageService.list(new CompanyManage(){{setCompanyId(Long.valueOf(communityById.get(0).getCompanyId()));}});
                    upkeepInfo.setMaintenanceUnit(companyById.get(0).getCompanyName());
                    upkeepInfo.setUpkeepType(elevatorPlan.getUpkeepType());
                    upkeepInfo.setUpkeepState("1");//保养状态
                    //维保人
                    String applicant = "";
                    List<SysUser> sysUserList = userService.selectUserListByAttrId(companyById.get(0).getCompanyId());
                    for (SysUser sysUser : sysUserList) {
                        String s = userService.selectUserRole(sysUser.getUserId());
                        if(s.contains(BusinessConstants.KEEP_ENGINEER)){
                            applicant = applicant + sysUser.getUserId() + ",";
                        }
                    }
                    upkeepInfo.setApplicant(applicant);
                    //截止时间
                    upkeepInfo.setOverTime(DateUtils.getDate(DateUtil.parseDate(recentlyTime), elevatorPlan.getUpkeepInterval()));
                    upkeepInfo.setLongitude(elevatorInfo.getLongitude());
                    upkeepInfo.setLatitude(elevatorInfo.getLatitude());
                    upkeepInfoService.save(upkeepInfo);
                }

            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
        return 1;
    }


}
